package com.jianjun.project.system.dict;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.Method;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jianjun.common.constant.HttpStatus;
import com.jianjun.common.util.SecurityUtils;
import com.jianjun.framework.web.domain.AjaxResult;
import com.jianjun.framework.web.interceptor.DataScope;
import com.jianjun.framework.web.page.TableDataInfo;
import com.jianjun.framework.web.render.ExcelWriterRender;
import com.jianjun.project.common.model.SysDictType;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据字典信息
 *
 * @author source
 */
@Path(value = "/system/dict/type")
public class SysDictTypeController extends Controller {
    @Inject
    SysDictTypeService dictTypeService;

    @DataScope(permission = "system:dict:list")
    public void list() {
        SysDictType dictType = JsonKit.parse(getKv().toJson(), SysDictType.class);
        Page<SysDictType> page = dictTypeService.paginate(dictType);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotalRow());
        renderJson(rspData);
    }

    @DataScope(permission = "system:dict:export")
    public void export() {
        SysDictType dictType = JsonKit.parse(getKv().toJson(), SysDictType.class);
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        List<Map<String, Object>> mapList = list.stream().map(Model::toMap).collect(Collectors.toList());
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(mapList, true);
        render(new ExcelWriterRender(writer, dictType.getClass().getSimpleName()));
    }

    @DataScope(permission = "system:dict:query,system:dict:add,system:dict:edit,system:dict:remove")
    public void index() {
        String method = getRequest().getMethod();
        //查询
        if (Method.GET.name().equals(method)) {
            Long dictId = getParaToLong();
            renderJson(AjaxResult.success(dictTypeService.selectDictTypeById(dictId)));
        }
        //新增
        if (Method.POST.name().equals(method)) {
            SysDictType dict = JsonKit.parse(getRawData(), SysDictType.class);
            if (!dictTypeService.checkDictTypeUnique(dict)) {
                renderJson(AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，参数键名已存在"));
                return;
            }
            dict.setCreateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(dictTypeService.insertDictType(dict)));
        }
        //修改
        if (Method.PUT.name().equals(method)) {
            SysDictType dict = JsonKit.parse(getRawData(), SysDictType.class);
            if (!dictTypeService.checkDictTypeUnique(dict)) {
                renderJson(AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，参数键名已存在"));
                return;
            }
            dict.setUpdateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(dictTypeService.updateDictType(dict)));
        }
        //删除
        if (Method.DELETE.name().equals(method)) {
            dictTypeService.deleteDictTypeByIds(Convert.toLongArray(getPara()));
            renderJson(AjaxResult.success());
        }
    }


    /**
     * 刷新字典缓存
     */
    @DataScope(permission = "system:dict:remove")
    public void refreshCache() {
        dictTypeService.resetDictCache();
        renderJson(AjaxResult.success());
    }

    /**
     * 获取字典选择框列表
     */
    public void optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        renderJson(AjaxResult.success(dictTypes));
    }
}
